package listeners;

import java.util.LinkedList;

public class Cook extends Thread 
{
	// COOK STUFF    
    private final int MAX_SLEEP = 5000;
    private final int MAX_COOKIES = 25;    
    
    // LISTENER STUFF    
    private LinkedList<CookListener> cookListeners = new LinkedList<CookListener>();
        
    private void cook(){
        int cookies = (int)(Math.random()*MAX_COOKIES-1)+1;    
        
        for(CookListener t: cookListeners){
            t.cookiesBaked(new CookieEvent(cookies));
        }
    }
    
    // COOK THREAD
    public void run()
    {        
        while(true) {            
            try 
            {
                sleep((int)(Math.random()*MAX_SLEEP));
            } 
            catch (InterruptedException e) 
            {    
            	
            }
            
            cook();
        }
    }    
     
    public void addCookListener(CookListener listener){
    	cookListeners.add(listener);
    }
    
    public boolean removeCookListener(CookListener listener){
        return cookListeners.remove(listener);
    }    
}
